/*
 * Copyright (C) 2010 The Android Open Source Project 
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at 
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and 
 * limitations under the License.
 */
package com.freeforall;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.View;

/**
 * Draws the actual game onto the layout.
 * @author Erik Buchholz
 * @author Mike Peterson
 */
public class GameView extends View{
	
	private Game game;
	
	private Paint cell;
	private Paint background;
	private int cellWidth;
	private int cellHeight;
	private int startX;
	private int startY;
	private boolean specsSet;
	private Bitmap[] bitmaps;
	private Rect destRect;
	
	private RefreshHandler redrawHandler = new RefreshHandler();

	/** 
	 * Use a handler to constantly tell the View to redraw. 
	 * Source: Project 2.
	 */
    class RefreshHandler extends Handler {

        @Override
        public void handleMessage(Message message) {
            GameView.this.update();
            GameView.this.invalidate();
        }

        public void update() {
            this.removeMessages(0);
            sendMessage(obtainMessage(0));
        }
    };

	/** Calls View's constructor and initializes the view. */
	public GameView(Context context, AttributeSet attrs){
		super(context, attrs);
	}
	
	/** Set up paints ahead of time. */
	public void init(Game game){
		startX = 0;
		startY = 0;
		specsSet = false;
		this.game = game;
		
		cell = new Paint(Paint.ANTI_ALIAS_FLAG);
		background = new Paint(Paint.ANTI_ALIAS_FLAG);
		
		cell.setColor(0xff888888);
		background.setColor(0xffd7d7d7);
		update();
		
		bitmaps = new Bitmap[4];
		bitmaps[0] = BitmapFactory.decodeResource(getResources(), R.drawable.red);
		bitmaps[1] = BitmapFactory.decodeResource(getResources(), R.drawable.green);
		bitmaps[2] = BitmapFactory.decodeResource(getResources(), R.drawable.blue);
		bitmaps[3] = BitmapFactory.decodeResource(getResources(), R.drawable.yellow);
		destRect = new Rect();
	}
	
	/** Draw the game board. */
	@Override
	protected void onDraw(Canvas canvas){
		super.onDraw(canvas);
		if(!specsSet){
			if (getHeight() < getWidth()){
				cellWidth = getHeight() / 7;
				cellHeight = getHeight() / 7;
				startY = 0;
				startX = (getWidth() - getHeight()) / 2;
			}
			else{
				cellWidth = getWidth() / 7;
				cellHeight = getWidth() / 7;
				startX = 0;
				startY = (getHeight() - getWidth()) / 2;
			}
			specsSet = true;
		}
		
		int[][] colorGrid = game.getColorGrid();
		for (int y = 0; y < Game.GRID_SIZE; y++){
			for (int x = 0; x < Game.GRID_SIZE; x++){
				destRect.left = cellWidth* x + startX;
				destRect.top = cellHeight * y + startY;
				destRect.right = (cellWidth * x) + cellWidth - 1 + startX;
				destRect.bottom = (cellHeight * y) + cellHeight - 1 + startY;
				switch(colorGrid[y][x]){
				default:
					canvas.drawRect(destRect, cell);
					break;
				case 0xffff0000:
					canvas.drawBitmap(bitmaps[0], null, destRect, null);
					break;
				case 0xff00ff00:
					canvas.drawBitmap(bitmaps[1], null, destRect, null);
					break;
				case 0xff0000ff:
					canvas.drawBitmap(bitmaps[2], null, destRect, null);
					break;
				case 0xffffff00:
					canvas.drawBitmap(bitmaps[3], null, destRect, null);
					break;
				}
			}
		}
	}
	
	/** Returns the x index of the cell clicked. */
	public int getXCell(int x){
		if (x - startX < 0) return -1;
		int xCell = (int)((x - startX)/ cellWidth);
		return xCell;
	}
	
	/** Returns the y index of the cell clicked. */
	public int getYCell(int y){
		if (y - startY < 0) return -1;
		int yCell = (int)((y - startY)/ cellHeight);
		return yCell;
	}
	
	/** Updates constantly. */
	public void update(){
		redrawHandler.update();
	}
}
